﻿var Task = function (title, priority, status, percentage, assigned, description, startdate, duedate) {
    this.title = title;
    this.priority = priority.toString().substring(4);
    this.status = status;
    this.percentage = percentage;
    this.assigned = assigned;
    this.description = description;
    this.startdate = startdate;
    this.duedate = duedate;
}

var taskModel = {
    tasks: ko.observableArray([])
};

function start() {
    // bind the data from the service call to the observableArray
    ko.dependentObservable(function () {
        $.getJSON("/_vti_bin/listdata.svc/Tasks").then(function (tasks) {
            $.each(tasks.d.results, function (i, task) {
                var t = new Task(task.Title, task.PriorityValue, task.StatusValue, task.Complete, task.AssignedToId, task.Description, task.StartDate, task.DueDate);
                taskModel.tasks.push(t);
            });
        })
        .error(function (data) {
            $("#debug").append("<div style='color:red'>error message :<br/><i>" + data.statusText + "</i></div>");
            $("#debug").append("<div style='color:red;font-weight:bold'>Returned JSON (please validate using http://jsonlint.com/ :</div>");
            $("#debug").append("<br/><hr/>");
            $("#debug").append(data.responseText);
        })
    }, taskModel);

    ko.applyBindings(taskModel);


}

$(document).ready(start);